//
//  MyCenterViewModel.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/9/27.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "MyCenterViewModel.h"

#import "UserCenterInfoData.h"

@interface MyCenterViewModel ()

@property (nonatomic, copy) NSString* coinNum;
@property (nonatomic, copy) NSString* collectionNum;
@property (nonatomic, copy) NSString* cosultNum;

@end

@implementation MyCenterViewModel

#pragma mark - Ovrride
- (void)initialize
{
    [super initialize];
    
    @weakify(self)
    
    _coinNum = @"0";
    _collectionNum = @"0";
    _cosultNum = @"0";
    
    _checkinCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            
            [subscriber sendNext:nil];
            [subscriber sendCompleted];
            
            return nil;
        }];
    }];
    
    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    [[RACObserve(um, userCenterInfoData) distinctUntilChanged] subscribeNext:^(id x)
    {
        @strongify(self);
        
        UserCenterInfoData* curData = x;
        
        self.coinNum = curData.money;
        self.collectionNum = curData.collect;
        self.cosultNum = curData.consult;
    }];
}

- (RACSignal*)_requestUserCenterDataSignal
{
    @weakify(self);
    
    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
    {
        [[AFNetWorkUtils racPOSTWthURL:url_interface_usertop params:interface_param_usertop(um.userData.userID)] subscribeNext:^(id x)
        {
            UserCenterInfoData* ucInfoData = [UserCenterInfoData modelWithJSON:x];
            
            [subscriber sendNext:ucInfoData];
            [subscriber sendCompleted];
        } error:^(NSError *error)
        {
            [subscriber sendCompleted];
        }];
        
        return nil;
    }];
}

@end
